﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using SWDataDB.BLL;


public partial class AdminManage_LoginIndex : System.Web.UI.Page
{
     
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Form.DefaultButton = "submitForm";  
        if (!Page.IsPostBack)
        {
           
        }
    }
    
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
       
    }
    #region 调用下面的方法实现客户端保存Cookie验证模式
    private void ValidateMethod()
    {
        if (Request.Cookies["CheckCode"] == null)
        {
            litErrorMsg.Text = "您的浏览器设置已被禁用 Cookies，您必须设置浏览器允许使用 Cookies 选项后才能使用本系统。";
            litErrorMsg.Visible = true;
            return;
        }
        if (String.Compare(Request.Cookies["CheckCode"].Value, Request.Form["txtVali"].ToString().Trim(), true) != 0)
        {
            litErrorMsg.Text = "<font color=red>对不起，验证码错误！</font>";
            litErrorMsg.Visible = true;
            return;
        }
        else
        {
            litErrorMsg.Text = "<font color=green>恭喜，验证码输入正确！</font>";
            litErrorMsg.Visible = true;
        }
    }
    #endregion 

    

    protected void submitForm_Click(object sender, EventArgs e)
    {
        if (Session["ValidateCode"] != null)
        {
            string outputValidateCode = Session["ValidateCode"] as string;
            string inputValidateCode = Request.Form["txtVali"].Trim();
            if (string.Compare(outputValidateCode, inputValidateCode, true) != 0)
            {
                //Response.Write("<script>javascript:alert('输入的验证码错误！');</script>");
                litErrorMsg.Text = "输入的验证码错误！";
            }
            else
            {
                //Response.Write("<script>javascript:alert('输入的验证码正确！');</script>");
                //litErrorMsg.Text = "输入的验证码正确！";
                UserManange LoginIn = new UserManange();


                string strUserGroup = Request.Form["UserGroup"];
                Int32 UserGroup = Convert.ToInt32(strUserGroup);

                string strUsername = this.username.Text;
                string strPwd = this.pwd.Text;

                short login = LoginIn.IdentifyUser(UserGroup, strUsername, strPwd);
                switch (login)
                {
                    case 1:
                        Session["UserName"] = this.username.Text;
                        Session["PassWord"] = this.pwd.Text;
                        Page.Response.Redirect("ManageIndex.aspx");
                         
                        break;
                    case 0:
                        litErrorMsg.Text = "输入的密码有误！";
                        break;
                    case -1:
                        litErrorMsg.Text = "用户角色选择错误！请重新选择！";
                        break;
                    default:
                        litErrorMsg.Text = "没有该用户，请联系管理员！";

                        break;
                }
                
            }
        }

    }
    protected void cbxRemeberUser_CheckedChanged(object sender, EventArgs e)
    {
        //创建一个Cookie对象
        HttpCookie cookie = new HttpCookie("UserName");
        //判断CheckBox控件是否被选中
        if (this.cbxRemeberUser.Checked)
        {
            cookie.Value = this.username.Text;
        }
        //获取创建的Cookie对象的过期时间
        cookie.Expires = DateTime.MaxValue;
        //将创建的Cookie对象添加到内部Cookie集合中
        Response.AppendCookie(cookie);
    }
}